package com.cyys.modules.system.controller.recycle;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.utils.StringUtil;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.system.model.recycle.SysRecycle;
import com.cyys.modules.system.service.param.ParamBasicService;
import com.cyys.modules.system.service.recycle.SysRecycleService;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/recycle")
public class SysRecycleAction extends BaseAction {

	@Resource
	private SysRecycleService sysRecycleService;

	@Resource
	private ParamBasicService paramBasicService;

	@GetMapping("/list")
	@RequiresPermissions("sys:recycle:list")
	public String list(Model model) {
		return "module/system/recycle/sysRecycle-list";
	}

	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("sys:recycle:list")
	public String listInfo(SysRecycle sysRecycle, Page page)  {
		QueryWrapper qw = new QueryWrapper(sysRecycle);
		qw.orderByDesc("is_open");
		qw.orderByDesc("table_name");
		return JSONData.toLayerData(sysRecycleService.list(page , qw,null));
	}

	@GetMapping("/listResource")
	@RequiresPermissions("sys:recycle:recovery")
	public String listResource(Model model) {
		model.addAttribute("tableCommentMap", sysRecycleService.getAllTableComment());
		return "module/system/recycle/recycle-data";
	}

	@ResponseBody
	@PostMapping("/listResource/listInfo")
	@RequiresPermissions("sys:recycle:recovery")
	public String listResourceInfo(String tableName, String queryParams, Page page)  {
		return JSONData.toLayerData(sysRecycleService.listResourceInfo(page, tableName, queryParams));
	}

	@ResponseBody
	@PostMapping("/listResource/getCols")
	@RequiresPermissions("sys:recycle:recovery")
	public AR listCols(String tableName)  {
		if(StringUtil.isBlank(tableName)){
			return AR.failed("缺少参数【tableName】");
		}
		return AR.ok(new HashedMap(){{
			put("queryColList", sysRecycleService.getQueryColList(tableName));//返回查询列信息
			put("colList", sysRecycleService.listTableColumn(tableName));//返回所有列数据
		}},"获取成功");
	}

	/**
	 * 数据恢复
	 * @param id
	 * @param tableName
	 * @return
	 * @
	 */
	@ResponseBody
	@PostMapping("/recovery")
	@RequiresPermissions("sys:recycle:recovery")
	public AR dataRecovery(String id,String tableName) {
		return sysRecycleService.dataRecovery(id, tableName);
	}

	/**
	 * 数据删除
	 * @param id
	 * @param tableName
	 * @return
	 * @
	 */
	@ResponseBody
	@PostMapping("/deletion")
	@RequiresPermissions("sys:recycle:recovery")
	public AR dataDeletion(String id, String tableName) {
		return sysRecycleService.dataDeletion(id, tableName);
	}

	@ResponseBody
	@RequestMapping("/updateTableComment")
	@RequiresPermissions(value = "sys:recycle:edit")
	public AR updateTableComment(SysRecycle entity)  {
		if(StringUtil.isBlank(entity.getId())){
			return AR.failed("缺少参数【id】");
		}
		return sysRecycleService.update(entity);
	}

	@ResponseBody
	@RequestMapping("/updateIsOpen")
	@RequiresPermissions(value = "sys:recycle:edit")
	public AR updateIsOpen(SysRecycle entity)  {
		if(StringUtil.isBlank(entity.getId())){
			return AR.failed("缺少参数【id】");
		}
		return sysRecycleService.update(entity);
	}

	@ResponseBody
	@RequestMapping("/initSystemRecycle")
	@RequiresPermissions(value = "sys:recycle:init")
	public AR initSystemRecycle()  {
		return sysRecycleService.initSysResycle();
	}


	@GetMapping("/setQueryCol")
	public String setQueryCol(Model model, String tableName) {
		model.addAttribute("tableName",tableName);
		model.addAttribute("queryInputTypeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.QUERY_INPUT_TYPE));
		model.addAttribute("sysParamMap", ParamBasicUtil.getParamBasic());
		return "module/system/recycle/queryCol-set";
	}

	@ResponseBody
	@PostMapping("/listForSetQueryCol")
	public String listForSetQueryCol(String tableName) {
		List<Map> columnList= sysRecycleService.listForSetQueryCol(tableName);
		return JSONData.toLayerDataNoPage(columnList);
	}

	/**
	 * 设置查询列
	 */
	@ResponseBody
	@RequestMapping("/setQueryColInfo")
	public AR setQueryColInfo(String isQueryCol, String tableName, String columnName, String columnComment)  {
		return sysRecycleService.setQueryCol(isQueryCol, tableName, columnName, columnComment);
	}

	/**
	 * 编辑查询列信息
	 */
	@ResponseBody
	@RequestMapping("/editQueryColInfo")
	public AR editQueryColInfo(String tableName, String columnName, String queryColInfo)  {
		return sysRecycleService.editQueryColInfo(tableName, columnName, queryColInfo);
	}
}
